It struck me that nature's
decimal 1000 100 10 1hexadecimal 4096 256 16 1
binary 8 4 2 1
octal 512 64 8 1
256 128 64 32 16 8 4 2 1to octal:128 64 32 16 8 4 2 1
(1*128)+(0*64)+(1*32)+(1*16)+(0*8)+(0*4)+(0*2)+(1*1)
10110001
512 64 8 1
64 8 1
(2*64)+(6*8)+(1*1)to hexadecimal:261
256 16 116 1
(11*16)+(1*1)
(B*16)+(1*1)
B1
512 256 128 64 32 16 8 4 2 1One's complement: 001011110256 128 64 32 16 8 4 2 1
(1*256)+(1*128)+(0*64)+(1*32)+(0*16)+(0*8)+(0*4)+(0*2)+
(1*1)
110100001
110100001
001011111---------
000000000
decimal 1000 100 10 1hexadecimal ... 256 ... ...
binary ... ... ... ...
octal 512 ... 8 ...
decimal 1000 100 10 1base 6 ... ... 6 ...
base 13 ... 169 ... ...
base 3 27 ... ... ...
Binary Number 100011010001To see how the binary number converts easily to octal, simply break the 12-digit binary number into groups ofOctal equivalent 4321
Hexadecimal equivalent 8D1
100 011 010 001Notice that the octal digit you have written under each group of thee bits corresponds precisely to the octal equivalent of that 3-digit binary number as shown in4 3 2 1
1000 1101 0001Notice that the hexadecimal digit you have written under each group of four bits corresponds precisely to the hexadecimal equivalent of that 4-digit binary number as shown in8 D 1
Positional values: 64 32 16 8 4 2 1
Positional values: 32 16 8 4 2 1Next we work from the leftmost column to the right. We divide 32 into 57 and observe that there is one 32 in 57 with a remainder of 25, so we write 1 in the 32 column. We divide 16 into 25 and observe that there is one 16 in 25 with a remainder of 9 and write 1 in the 16 column. We divide 8 into 9 and observe that there is one 8 in 9 with a remainder of 1. The next two columns each produce quotients of zero when their positional values are divided into 1 so we write 0s in the 4 and 2
Positional values: 32 16 8 4 2 1and thus decimal 57 is equivalent to binary 111001.Symbol values: 1 1 1 0 0 1
Positional values: 512 64 8 1
Positional values: 64 8 1Next we work from the leftmost column to the right. We divide 64 into 103 and observe that there is one 64 in 103 with a remainder of 39, so we write 1 in the 64 column. We divide 8 into 39 and observe that there are four 8s in 39 with a remainder of 7 and write 4 in the 8 column. Finally, we divide 1 into 7 and observe that there are seven 1s in 7 with no remainder so we write 7 in the 1 column. This yields:
Positional values: 64 8 1
Symbol values: 1 4 7and thus decimal 103 is equivalent to octal 147.
Positional values: 4096 256 16 1Then we discard the column with positional value 4096, yielding:
Positional values: 256 16 1
Positional values: 256 16 1and thus decimal 375 is equivalent to hexadecimal 177.Symbol values: 1 7 7
int value = 13;
00000000 00000000 00000000 00001101To form the negative of value we first form its one's complement by applying C++'s bitwise complement operator (~):
onesComplementOfValue = ~value;Internally, ~value is now value with each of its bits reversed--ones become zeros and zeros become ones as follows:
00000000 00000000 00000000 00001101
11111111 11111111 11111111 11110010To form the two's complement of value we simply add one to value's one's complement. Thus
11111111 11111111 11111111 11110011Now if this is in fact equal to -13, we should be able to add it to binary 13 and obtain a result of 0. Let us try this:
00000000 00000000 00000000 00001101The carry bit coming out of the leftmost column is discarded and we indeed get zero as a result. If we add the one's complement of a number to the number, the result would be all 1s. The key to getting a result of all zeros is that the two's complement is 1 more than the one's complement. The addition of 1 causes each column to add to 0 with a carry of 1. The carry keeps+11111111 11111111 11111111 11110011
------------------------------------
00000000 00000000 00000000 00000000
x = a - value;by adding the two's complement of value to a as follows:
x = a + (~value + 1);Suppose a is 27 and value is 13. If the two's complement of value is actually the negative of value, then adding the two's complement of value to a should produce the result 14. Let us try this:
00000000 00000000 00000000 00011011+(~value + 1)
+11111111 11111111 11111111 11110011which is indeed equal to 14.------------------------------------
00000000 00000000 00000000 00001110